package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.utils.Constant;

public class FreePassPortActovity extends BaseFragmentActivity implements
		OnClickListener
{

	private TextView title;
	private ImageView ivVerify;
	private Button btnVerify;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.free_passport);
		title = (TextView) findViewById(R.id.top_text);
		title.setText("通行证");
		initViews();
		initEvents();
	}

	@Override
	protected void initViews()
	{
		// TODO Auto-generated method stub
		ivVerify = (ImageView) findViewById(R.id.ivVerify1);
		btnVerify = (Button) findViewById(R.id.btnVerify);
	}

	@Override
	protected void initEvents()
	{
		// TODO Auto-generated method stub
		ivVerify.setOnClickListener(this);
		btnVerify.setOnClickListener(this);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		if (isLogin())
		{
			getAttestationMsg();
		}
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.ivVerify1:
			break;
		case R.id.btnVerify:
			if (!isLogin())
			{
				Intent intent = new Intent(this, RegisterActivity.class);
				startActivity(intent);
			} else
			{
				Intent intent = new Intent(this, VerifyActivity.class);
				intent.putExtra("type", 0);
				startActivity(intent);
			}
			break;
		}
	}

	/** 进行认证 */
	public void getAttestationMsg()
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "67");
		params.put("phone", mApplication.user.getPhone());
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					System.out.println(json);
					JSONObject object = new JSONObject(json);
					String code = object.getString("code");
					String status = object.getString("status");
					if (status.contains("0"))
					{
						ivVerify.setVisibility(View.VISIBLE);
						btnVerify.setVisibility(View.GONE);
						imageLoader.displayImage(Constant.POST1 + code,
								ivVerify, options);
					} else
					{
						ivVerify.setVisibility(View.VISIBLE);
						btnVerify.setVisibility(View.VISIBLE);
					}
				} catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		});
	}
}
